<template>
  <div class="login-container">
    <div class="login-card">
      <div class="logo-wrapper">
        <el-icon class="logo-icon"><Coffee /></el-icon>
      </div>

      <h2 class="login-title">奶茶培训系统</h2>

      <el-form
          ref="ruleFormRef"
          :model="login"
          :rules="rules"
          class="login-form"
          @keyup.enter="loginAdmin"
      >
        <el-form-item prop="username">
          <el-input
              v-model="login.username"
              placeholder="请输入账号"
              size="large"
              :prefix-icon="User"
              clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              v-model="login.password"
              placeholder="请输入密码"
              type="password"
              size="large"
              :prefix-icon="Lock"
              show-password
              clearable
          />
        </el-form-item>

        <el-form-item prop="code" class="code-form-item">
          <el-input
              v-model="login.code"
              placeholder="请输入验证码"
              size="large"
              :prefix-icon="Key"
              clearable
              class="code-input"
          />
          <div class="code-image-wrapper">
            <el-image
                :src="codeImg"
                class="code-image"
                fit="cover"
                @click="getCode"
            />
          </div>
        </el-form-item>

        <el-form-item class="button-group">
          <el-button
              type="primary"
              size="large"
              @click="loginAdmin"
              class="login-btn"
              :loading="loading"
          >
            登录
          </el-button>
          <el-button
              size="large"
              @click="resetForm"
              class="reset-btn"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage, type FormInstance } from 'element-plus'
import { User, Lock, Key, Coffee } from '@element-plus/icons-vue'  // 新增咖啡图标
import { useAuthStore } from "@/stores/auth.ts"
import { useRouter } from "vue-router"
import type { adminData } from "@/types/auth.ts"
import axios from "@/utils/request.ts"

const router = useRouter()
const authStore = useAuthStore()
const loading = ref(false)
const ruleFormRef = ref<FormInstance>()

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: ['blur', 'change'] },
    { min: 2, max: 20, message: '用户名长度在 2 到 20 个字符之间', trigger: ['blur', 'change'] }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
    { min: 2, max: 30, message: '密码长度在 2 到 30 个字符之间', trigger: ['blur', 'change'] }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: ['blur', 'change'] },
  ]
}

const login: adminData = reactive({
  username: '',
  password: '',
  code: '',
  codeKey: '',
})

const codeImg = ref("")

const getCode = () => {
  axios.get("/common/code").then(
      response => {
        codeImg.value = "data:image/jpeg;base64," + response.data.codeImg
        login.codeKey = response.data.codeKey
      }
  )
}

const loginAdmin = async () => {
  // 先进行表单验证
  if (!ruleFormRef.value) return
  await ruleFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const isSuccess = await authStore.login(login)
        if (isSuccess.success) {
          ElMessage.success('登录成功')
          router.push('/')
        } else {
          ElMessage.error('登录失败，请检查账号密码或验证码')
          getCode() // 刷新验证码
        }
      } catch (error) {
        ElMessage.error('登录异常，请稍后重试')
        getCode() // 刷新验证码
      } finally {
        loading.value = false
      }
    }
  })
}

const resetForm = () => {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields()
  }
  getCode()
}

getCode()
</script>

<style scoped>

/* 奶茶主题 - 登录容器：淡奶色渐变背景 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #fff5e6 0%, #ffe8cc 100%);
  padding: 20px;
}

/* 登录卡片：奶白色主体，柔和阴影 */
.login-card {
  width: 100%;
  max-width: 420px;
  background: #fffdfa;
  border-radius: 16px;
  box-shadow: 0 12px 35px rgba(250, 204, 153, 0.2);
  padding: 45px 35px;
  border: 1px solid #ffe0b2;
}

/* 品牌Logo样式 */
.logo-wrapper {
  text-align: center;
  margin-bottom: 20px;
}
.logo-icon {
  font-size: 48px;
  color: #e67e22;
  filter: drop-shadow(0 2px 4px rgba(230, 126, 34, 0.2));
}

/* 标题样式 */
.login-title {
  text-align: center;
  margin-bottom: 35px;
  color: #e67e22;
  font-weight: 700;
  font-size: 26px;
  position: relative;
}
.login-title::after {
  content: '';
  display: block;
  width: 60px;
  height: 3px;
  background: #ffcc80;
  border-radius: 2px;
  margin: 8px auto 0;
}

/* 表单容器 */
.login-form {
  width: 100%;
}

/* 验证码区域样式 */
.code-form-item {
  display: flex;
  gap: 12px;
  align-items: center;
}

.code-input {
  flex: 1;
}

.code-image-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 130px;
  background: #fff8f0;
  padding: 5px;
  border-radius: 8px;
  border: 1px solid #ffe0b2;
}

.code-image {
  width: 100%;
  height: 42px;
  border-radius: 6px;
  border: 1px solid #ffcc80;
  cursor: pointer;
  transition: all 0.3s ease;
}
.code-image:hover {
  transform: scale(1.02);
  box-shadow: 0 2px 8px rgba(255, 152, 0, 0.15);
}

/* 刷新提示文字 */
.refresh-tip {
  font-size: 12px;
  color: #f39c12;
  margin-top: 6px;
  cursor: pointer;
  transition: color 0.2s;
}
.refresh-tip:hover {
  color: #e67e22;
}

/* 按钮组样式 */
.button-group {
  margin-top: 35px;
  display: flex;
  gap: 18px;
}
/* 登录按钮 */
.login-btn {
  flex: 2;
  background: #e67e22 !important;
  border-color: #e67e22 !important;
  color: white !important;
  border-radius: 8px !important;
  font-weight: 600 !important;
}
.login-btn:hover {
  background: #d35400 !important;
  border-color: #d35400 !important;
}
/* 重置按钮 */
.reset-btn {
  flex: 1;
  border-color: #ffcc80 !important;
  color: #e67e22 !important;
  border-radius: 8px !important;
}
.reset-btn:hover {
  background: #fff8f0 !important;
}

/* 输入框样式调整 */
:deep(.el-input__prefix-icon) {
  color: #f39c12 !important;
  font-size: 18px !important;
}
:deep(.el-input__wrapper) {
  border-radius: 8px !important;
  border-color: #ffe0b2 !important;
}
:deep(.el-input__wrapper:focus-within) {
  border-color: #f39c12 !important;
  box-shadow: 0 0 0 2px rgba(243, 156, 18, 0.1) !important;
}

/* 密码显示按钮样式 */
:deep(.el-input__suffix) {
  color: #f39c12 !important;
}
</style>
